package game{
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.collections.ArrayCollection;
	
	public class GameObject extends Shape{
//		protected var x:Number=0;
//		protected var y:Number=0;
		
		protected var lastX:Number;
		protected var lastY:Number;
		
		//uint for speed is pixel per second
		protected var speedX:Number = 0;
		protected var speedY:Number = 0;
		
		//unit for acceleration is pixel per second^2
		protected var acceX:Number = 0;
		protected var acceY:Number = 0;
		
		protected var rotateAngle:Number = 0;
		
		protected var image:BitmapData;
		
		protected var g:Game;
		protected var maxSpeed:int = 100;
		
		public var path:ArrayCollection = new ArrayCollection();
		public function GameObject(game:Game){
			this.g = game;
		}
		
		public function rotate(angle:Number):void{
			path.removeAll();
			rotateAngle += angle;
		}
		
		public function tick(time:Number):void{
			prepareCalc(time);
			calc(time);
		}
		
		protected function prepareCalc(time:Number):void{
		}
		
		protected function calc(time:Number):void{
			var dx:Number = speedX * time / 1000 + acceX * time / 1000 * time / 1000 / 2;
			var dy:Number = speedY * time / 1000 + acceY * time / 1000 * time / 1000 / 2;
			
			speedX += acceX * time;
			speedY += acceY * time;

			speedX = Math.max(-maxSpeed , speedX);			
			speedX = Math.min(maxSpeed , speedX);
			
			speedY = Math.max(-maxSpeed , speedY);
			speedY = Math.min(maxSpeed , speedY);
			
			x += dx;
			y += dy;
			
			if(y <= -image.height){
				y = g.getScreenHeight();
			}
			
			if(y > g.getScreenHeight() + image.height){
				y = -image.height;
			}
			
			if(x <= - image.width ){
				x = g.getScreenWidth();
			}
			
			if(x > g.getScreenWidth()){
				x = - image.width;
			}
			
			if(path.length > 15){
				path.removeItemAt(0);
			}
			
			if(path.length > 0){
				var lp:Point = Point(path.getItemAt(path.length-1));
				if(lp.x != x || lp.y != y){
					path.addItem(new Point(x,y));					
				}
			}else{
				path.addItem(new Point(x,y));
			}
		}
		
		public function getX():int{
			return x;
		}
		
		public function getY():int{
			return y;
		}
		
//		public function setX(x:int):void{
//			this.x = x;
//		}
//		
//		public function setY(y:int):void{
//			this.y = y;
//		}
		
		protected function prepareDraw():void{
		}
		
		public function drawToBuffer(buffer:BitmapData):void{
			prepareDraw();
			if(image){
				var m:Matrix = new Matrix();
				m.tx = x;
				m.ty = y;
//				m.tx -= image.width / 2;
//				m.ty -= image.height / 2;
//				
//				m.rotate(angleToRadian(rotateAngle));
//				
//				m.tx += image.width / 2 + x;
//				m.ty += image.width / 2 + y;

				buffer.draw(image,m,null,null,null,true);
			}
			
		}
		
		public static function angleToRadian(angle:Number):Number{
			return angle * Math.PI / 180;
		}
		
		
	}
}